/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MouseEventHandler.h"

#include <Core/Interface/Input.h>
#include <Modules/Input/OIS/Manager.h>
#include <Modules/Input/OIS/Producer.h>

#include <Core/Kernel/AppContext.h>
#include <Core/Kernel/Galaxy.h>
#include <Core/Mediator/InterMediator.h>

namespace postime = boost::posix_time;

namespace galaxy {
    namespace input {
        namespace ois_input {

            MouseEventHandler::MouseEventHandler(boost::shared_ptr<Manager> manager)
                : m_Timer (postime::microsec_clock::universal_time())
            {
                m_Manager = manager;
            }

            MouseEventHandler::~MouseEventHandler(void)
            {
                m_Manager.reset();
            }

            bool MouseEventHandler::mouseMoved (OIS::MouseEvent const &e ) {
                input::MouseEvent event(mediator::CATEGORY_INPUT, 
                    MODULE_TYPE_INPUT_MOUSE, MODULE_SUBTYPE_INPUT_MOUSEMOVE);

                event.m_Xrel = e.state.X.rel;
                event.m_Yrel = e.state.Y.rel;

                postMouseEvent (event);

                return true;
            }

            bool MouseEventHandler::mousePressed (OIS::MouseEvent const &e, OIS::MouseButtonID id ) {
                input::MouseEvent event(mediator::CATEGORY_INPUT, 
                    MODULE_TYPE_INPUT_MOUSE, MODULE_SUBTYPE_INPUT_MOUSEPRESS);

                switch (id) {
                    case OIS::MB_Left:
                        event.m_MouseButton = input::MOUSE_BUTTON_LEFT;
                        break;
                    case OIS::MB_Middle:
                        event.m_MouseButton = input::MOUSE_BUTTON_MIDDLE;
                        break;
                    case OIS::MB_Right:
                        event.m_MouseButton = input::MOUSE_BUTTON_RIGHT;
                        break;
                    default:
                        event.m_MouseButton = input::MOUSE_BUTTON_NONE;
                }

                event.m_Xrel = e.state.X.rel;
                event.m_Yrel = e.state.Y.rel;

                postMouseEvent(event);

                return true;
            }

            bool MouseEventHandler::mouseReleased (OIS::MouseEvent const &e, OIS::MouseButtonID id ) {
                input::MouseEvent event(mediator::CATEGORY_INPUT, 
                    MODULE_TYPE_INPUT_MOUSE, MODULE_SUBTYPE_INPUT_MOUSERELEASE);

                switch (id) {
                    case OIS::MB_Left:
                        event.m_MouseButton = input::MOUSE_BUTTON_LEFT;
                        break;
                    case OIS::MB_Middle:
                        event.m_MouseButton = input::MOUSE_BUTTON_MIDDLE;
                        break;
                    case OIS::MB_Right:
                        event.m_MouseButton = input::MOUSE_BUTTON_RIGHT;
                        break;
                    default:
                        event.m_MouseButton = input::MOUSE_BUTTON_NONE;
                }

                postMouseEvent(event);

                return true;
            }



            bool MouseEventHandler::postMouseEvent (mediator::Event& event) {
                mediator::InterMediator::postEvent (event, mediator::POST_DELAYED);
                return true;
            }

        } // namespace ois_input
    } // namespace input
} // namespace galaxy
